﻿"use strict";
define(['admin/app', "service/admin/userFeedBackService"], function (app) {
    app.controller('userFeedBackController', function ($scope, $stateParams, $location, $uibModal, $state, utils, userFeedBackService) {
        $scope.query = {
            page: 1, size: 20, q: '', title: ''
        };
        $.extend($scope.query, $stateParams);
        var methods = {
            textAreaChanged: function (wordCount) {
                $scope.textcount = wordCount - $scope.model.reply_content.length;
                if ($scope.textcount <= 0) {
                    $scope.textcount = 0;
                    $("#textcount").css("color", "red")
                } else {
                    $("#textcount").css("color", "#ccc")
                }

                if ($scope.model.reply_content.length > wordCount) {
                    $scope.model.reply_content = $scope.model.reply_content.substr(0, wordCount);
                }
                $("#textcount").text($scope.model.reply_content.length + "/" + $scope.textcount);
            },
            search: function () {
                userFeedBackService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            detail: function () {
                if ($stateParams.id > 0) {
                    userFeedBackService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '查看';
                }
            },
            reply_detail: function () {
                if ($stateParams.id > 0) {
                    userFeedBackService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '回复';
                }
            },
            save: function () {
                var modal = utils.confirm({ msg: "您确定要保存么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    userFeedBackService.reply(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            goBack: function () {
                history.back();
            },
            search_page: function (page) {
                $scope.query.page = page;
                methods.search('page', $scope.query.page);
            },
        }

        angular.extend($scope, methods);
        if ($state.includes('app.userFeedBack.detail')) {
            methods.detail();
        } else if ($state.includes('app.userFeedBack.reply_detail')) {
            methods.reply_detail();
        } else {
            methods.search();
        }
    });
});
